from PIL import Image
from PIL import ImageEnhance
import os
import cv2
import numpy as np
import random
from skimage.transform import rescale

# 旋转
def rotation(img):
    rotation_img = img.rotate(180) #旋转角度random.choice([90,180,270])
    return rotation_img

# 上采样
def random_crop(img, min_ratio=0.6, max_ratio=1.0):
    h, w = [57,83]
    ratio = random.random()
    scale = min_ratio + ratio * (max_ratio - min_ratio)
    new_h = int(h*scale)    
    new_w = int(w*scale)
    y = np.random.randint(0, h - new_h)    
    x = np.random.randint(0, w - new_w)
    img = img[y:y+new_h, x:x+new_w]
    resize_img = cv2.resize(img, (83, 57), interpolation = cv2.INTER_CUBIC)
    return resize_img

src_path = "E:/files/my_file/master/课件/图形图像处理/GT_data/resize_GT_train"
path = "C:/Users/lakuite/Desktop/tmp"

img_list = os.listdir(src_path)
for i, pic in enumerate(img_list):
    pic_path = src_path + '/' + pic
    img = Image.open(pic_path)
    dic_path = path + '/' + pic[:-4] + '_3.jpg'
#     saveImage = random_crop(np.array(img))    #1
#     cv2.imwrite(dic_path, saveImage)          #1
    saveImage = rotation(img)                 #2
    saveImage.save(dic_path)                  #2